setwd('/Users/qinkevin/PycharmProjects/An-introduction-to-analysis-of-financial-data-with-R')
da=read.table('d-c2c-0110.txt',header = T)
csco=log(da$CSCO+1)
cat=log(da$CAT+1)
library(fGarch)
m1=garchFit(~1+garch(1,1),data = csco,trace = F)
m2=garchFit(~1+garch(1,1),data=cat,trace = F)
vcsco=volatility(m1)
vcat=volatility(m2)
xp=csco+cat
xm=csco-cat
m3=garchFit(~1+garch(1,1),data = xp,trace=F)
vxp=volatility(m3)
m4=garchFit(~1+garch(1,1),data=xm,trace = F)
vxm=volatility(m4)
CoV=(vxp^2-vxm^2)/4
CoR=CoV/(vcat*vcsco)
source('EWMAvol.R')
da=read.table('d-sp500-0110.txt')
sp5=da[,1]
da=read.table('d-c2c-0110.txt',header = T)
cat=log(da$CAT+1)
xp=cat+sp5
xm=cat-sp5
m1=garchFit(~1+garch(1,1),data=xp,trace = F)
m2=garchFit(~1+garch(1,1),data=xm,trace = F)
m3=garchFit(~1+garch(1,1),data=sp5,trace = F)
vxp=volatility(m1)
vxm=volatility(m2)
vsp5=volatility(m3)
beta=(vxp^2-vxm^2)/(4*vsp5^2)
tdx=c(1:2515)/252+2001
m4=lm(cat~sp5)
plot(tdx,beta,xlab='year',ylab='beta',type='l')
abline(h=c(1.146))
idx=c(1:2515)[beta==max(beta)]
library(fGarch)
da=read.table('w-petroprice.txt',header = T)
pet=da$US
head(pet)
price=ts(pet,frequency = 52,start = c(1997,1))
dp=ts(diff(price),frequency = 52,start = c(1997,2))
par(mfcol=c(2,1))
plot(price,xlab='year',ylab='price')
plot(dp,xlab='year',ylab='changes')
cprice=diff(price)
m2=arima(cprice,order=c(3,0,0),seasonal = list(order=c(2,0,0),period=5),include.mean = F)
m2=arima(cprice,seasonal = list(order=c(2,0,0),period=5),include.mean = F)
adjcp=cprice[11:716]-0.0983*cprice[6:711]-0.1152*cprice[1:706]
acf(adjcp)
pacf(adjcp)
m3=garchFit(~arma(3,0)+garch(1,1),data=adjcp,trace = F,include.mean = F)
plot(m3)
m4=garchFit(~arma(3,0)+garch(1,1),data=adjcp,trace = F,include.mean = F,cond.dist = 'std')
plot(m4)
m5=garchFit(~arma(1,0)+garch(1,1),data=adjcp,trace = F,include.mean = F,cond.dist = 'sstd')
plot(m5)
M3=arima(adjcp,order = c(3,0,0),include.mean = F)
source('backtest.R')
M3F=backtest(M3,adjcp,650,2,inc.mean = F)
source('backtestGarch.R')
M4F=backtestGarch(adjcp,650,2,inc.mean = F,cdist = 'sstd')
